package com.hopu.v1.controller.admin.exam;

import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.v1.pojo.common.Delete;
import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.common.Save;
import com.hopu.v1.pojo.common.Update;
import com.hopu.v1.pojo.exam.ExamRecords;
import com.hopu.v1.service.admin.exam.ExamRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * ClassName: ExamRecordsController
 * Description:
 *
 * @Author lh
 * @Create 2024/9/24 17:14
 * @Version 1.0
 */
@RestController
@RequestMapping("/examRecords")
public class ExamRecordsController {
    @Autowired
    private ExamRecordsService examRecordsService;

    /**
     * 新增考试试卷及考题选项
     * @param examRecords
     * @return
     */
    @PostMapping("/addExam")
    public R addExam(@Validated(Save.class) @RequestBody ExamRecords examRecords) {
        examRecordsService.addExam(examRecords);
        return R.ok("添加成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/del")
    public R del(@Validated(Delete.class) Integer id) {
        System.out.println(id);
        examRecordsService.del(id);
        return R.ok("操作成功");
    }

    /**
     * 修改
     * @param examRecords
     * @return
     */
    @PostMapping("/update")
    public R update(@Validated(Update.class) @RequestBody ExamRecords examRecords) {
        examRecordsService.update(examRecords);
        return R.ok("操作成功");
    }

    /**
     * 分页查询
     * @param page
     * @param examRecords
     * @return
     */
    @GetMapping("/selectList")
    public R selectList(Page<ExamRecords> page, ExamRecords examRecords,String endTime) {
        LambdaQueryWrapper<ExamRecords> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isNullOrEmpty(examRecords.getExamName())){
            wrapper.like(ExamRecords::getExamName, examRecords.getExamName());
        }else if(examRecords.getExamStartTime() != null){
            wrapper.between(ExamRecords::getExamStartTime, examRecords.getExamStartTime(),endTime);
        }
        wrapper.eq(ExamRecords::getClassId, examRecords.getClassId());
        wrapper.orderByDesc(ExamRecords::getCreateTime);
        return R.ok(examRecordsService.page(page, wrapper));
    }

    /**
     * 发布
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/release")
    public R release(Integer id,Integer status) {
        examRecordsService.release(id,status);
        return R.ok("操作成功");
    }

    /**
     * 查询考试记录
     * @param id
     * @return
     */
    @GetMapping("/selectById")
    public R selectById(Integer id,String status,String correction ) {
        return R.ok(examRecordsService.selectById(id,status,correction));
    }
}
